<?php
namespace Admin\Controller;

class AdminController extends CommonController {
    public function _empty(){
        echo "<h1>404</h1>";
    }
    public function admin_list(){
        import('ORG.Util.Page');
        $count = M('admin')->count();
        $page = new \Think\Page($count,3);
        $show = $page->show();
        $data = M('admin')->order('id')->limit($page->firstRow.','.$page->listRows)->select();

        $list = M('auth_group')->field('title,id')->select();
        foreach ($data as $key => $value) {
            $group_title = M('admin')->table('dota2_auth_group_access a,dota2_auth_group g')->field('g.title')->where('g.id = a.group_id and a.uid = '.$value["id"])->find();
            $data[$key]['group_title'] = $group_title['title'];
        }
        $this->assign('page',$show);
        $this->assign('list',$list);
        $this->assign('data',$data);
        $this->show();
    }

    public function auth_group(){

        import('ORG.Util.Page');
        $count =  M('auth_group')->count();
        $page = new \Think\Page($count,1);
        $show = $page->show();
        $data =  M('auth_group')->order('id')->limit($page->firstRow.','.$page->listRows)->select();
        $this->assign('data',$data);
        $this->assign('page',$show);
        $this->show();
    }

    // 显示添加页面
    public function admin_add(){
        $data = M('auth_group')->field('title,id')->select();
        $this->assign('data',$data);
        $this->show();
    }

    public function check_account()
    {   
        $data = M('admin')->where('account="'.$_GET['account'].'"')->find();
        if ($data) {
            echo '1';
        }else{
            echo '0';
        }
    }

    //添加管理员
    public function admin_insert()
    {
        $p['account'] = $_POST['account'];
        $p['password'] = $_POST['password'];
        $p['create_time'] = time();
        $add = M('admin')->add($p);
        if ($add) {
            $uid = M('admin')->field('id')->where('account="'.$_POST['account'].'"')->find()['id'];
            // echo json_encode($uid);die;
            $data['uid']=$uid;
            $data['group_id']=$_POST['group_id'];
            if (!M('auth_group_access')->add($data)) {
                echo '绑定用户组失败';
            }else{echo '添加成功';};
        }else{echo '添加管理员失败';}
    }

    public function admin_edit()
    {
        $admin = M('admin');
        $admin->account = $_POST['account'];
        $admin->status = $_POST['status'];
        $res = M('admin')->where('id='.$_POST['id'])->save();
        $data['group_id'] = $_POST['group_id'];
        $res1 = M('auth_group_access')->where('uid='.$_POST['id'])->save($data);
        if ($res || $res1) {
            echo '修改成功';
        }else{
            echo '修改失败';
        }
    }

    public function admin_del()
    {
        $admin = M('admin')->where('id='.$_GET['id'])->delete();
        if ($admin ) {
            $group = M('auth_group_access')->where('uid='.$_GET['id'])->delete();
            echo '删除成功';
        }else{
            echo '删除失败';
        }
    }

    public function group_add(){
        $access = M('auth_rule')->where('status=1')->select();
        // var_dump(list_to_tree($access)[0]);die;
        $this->assign('access',list_to_tree($access));
        $this->display();
    }
    public function group_insert(){
        $rules = '';
        foreach ($_POST['rules'] as $v) {
            $rules .= $v.',';
        }
        $rules = rtrim($rules,',');
        $data['title'] = $_POST['title'];
        $data['rules'] = $rules;
        $data['create_time'] = time();
        $res = M('auth_group')->add($data);
        if ($res) {
            $this->success('添加成功',U('Admin/auth_group'));
        }else{
            $this->error('添加失败');
        }
    }

    public function group_edit()
    {
        $all_access = M('auth_rule')->where('status=1')->select();
        // var_dump(list_to_tree($all_access)[0]);die;
        $access = M('auth_group')->field('title,rules')->where('status = 1 and id='.$_GET['id'])->select()[0];
        $access['rules'] = explode(',',$access['rules']);
        $this->assign('access',$access);
        $this->assign('all_access',list_to_tree($all_access));
        $this->display();
    }

    public function group_update(){
        $rules = '';
        foreach ($_POST['rules'] as $v) {
            $rules .= $v.',';
        }
        $rules = rtrim($rules,',');
        $data['title'] = $_POST['title'];
        $data['rules'] = $rules;
        $res = M('auth_group')->where('id='.$_GET['id'])->save($data);
        if ($res) {
            $this->success('修改成功',U('Admin/auth_group'));
        }else{
            $this->error('修改失败');
        }
    }

    public function group_del()
    {
        $check = M('auth_group_access')->where('group_id='.$_GET['id'])->select();
        if (!empty($check)) {
            echo '无法删除含有成员的用户组';
        }else{
            $res = M('auth_group')->where('id='.$_GET['id'])->delete();
            if ($res) {
                echo '删除成功';
            }else{
                echo '删除失败';
            }
        }
    }

    public function auth_rule()
    {
        $data = M('auth_rule')->select();
        $data = getTree($data);
        $this->assign('data',$data);
        $this->display();
    }

    public function rule_add()
    {
        $data = I('post.');
        $data['create_time'] = time();
            $res = M('auth_rule')->add($data);
            if ($res) {
                $this->success('添加成功',U('Admin/auth_rule'));
            }else{
                $this->error('添加失败');
            }
    }

    public function rule_edit(){
        $data = I('post.');
        $data['update_time'] = time();
        $res = M('auth_rule')->where('id='.$_POST['id'])->save($data);
        if ($res) {
            echo '1';
        }else{
            echo '2';
        }
    }

}